package com.wehealth.util;


import com.wehealth.dm.User;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


public class DataManger{
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor editor;
    private static final String PREFERENCES_NAME= "WeHealth";
    
    private static final String  ID="userId";
    private static final String  ACCOUNT="account";
    private static final String  NAME="userName";
    private static final String  PSW="psw";
    private static final String  SEX="sex";
    private static final String  AGE="age";
    private static final String  PHONENUM="phoneNum";
    private static final String ADDRESS ="address";
    
    private static final String RCMID ="recommendID";
    
    
    
    public DataManger(Context context) {
    	mPreferences = context.getSharedPreferences(
    			PREFERENCES_NAME, Context.MODE_PRIVATE);
        editor = mPreferences.edit();
    }
    
    public void storeUserInfo(User user){
    	
    	int userId = user.getUserId();
    	String account =user.getAccount();
    	String psw = user.getPassword();
    	String userName = user.getUserName();
    	int userSex =user.getUserSex();
    	int userAge = user.getUserAge();
    	String userPhoneNum = user.getPhoneNumber();
    	String userAddress = user.getaddressr();
    	String rcmdID =user.getRecommendID();
    	
    	editor.putInt(ID, userId);
    	editor.putString(PSW, psw);
    	editor.putString(ACCOUNT, account);
    	editor.putString(NAME, userName);
    	editor.putInt(SEX, userSex);
    	editor.putInt(AGE, userAge);
    	editor.putString(PHONENUM, userPhoneNum);
    	editor.putString(ADDRESS, userAddress);
    	editor.putString(RCMID, rcmdID);
    	editor.commit();
    	Log.d("SIMMON","storeUserInfo    userId="+userId+" account= "+account+"   psw= "+psw+" userName="
    	+userName+"   userSex="+userSex+" userAge= "+userAge+"  userPhoneNum="+userPhoneNum+"  userAddress="+userAddress);
    }
    
    public User getUserInfo(){

    	int userId =mPreferences.getInt(ID, -1);
    	String psw =mPreferences.getString(PSW, null);
    	String account =mPreferences.getString(ACCOUNT, null);
    	String userName=mPreferences.getString(NAME, null);
    	int userSex=mPreferences.getInt(SEX, -1);
    	int userAge=mPreferences.getInt(AGE, -1);
    	String userPhoneNum=mPreferences.getString(PHONENUM, null);
    	String userAddress=mPreferences.getString(ADDRESS, null);
    	String rcmdID = mPreferences.getString(RCMID, "4");
    	Log.d("SIMMON","getUserInfo    userId="+userId+" account= "+account+"   psw= "+psw+" userName="
    	+userName+"   userSex="+userSex+" userAge= "+userAge+"  userPhoneNum="+userPhoneNum+"  userAddress="+userAddress);
    	
    	User user = new User();
    	user.setUserId(userId);
    	user.setAccount(account);
    	user.setPassword(psw);
    	user.setUserName(userName);
    	user.setUserSex(userSex);
    	user.setUserAge(userAge);
    	user.setPhoneNumber(userPhoneNum);
    	user.setaddress(userAddress);
    	user.setRecommendID(rcmdID);
    	return user;
    }
    
    
    public void  delUserDate(){
    	Log.d("SIMMON"," delUserDate");
    	editor.putInt(ID, -1);
    	editor.putString(PSW, null);
    	editor.putString(ACCOUNT, null);
    	editor.putString(NAME, null);
    	editor.putInt(SEX, -1);
    	editor.putInt(AGE, -1);
    	editor.putString(PHONENUM, null);
    	editor.putString(ADDRESS, null);
    	editor.putString(RCMID, null);
    	editor.commit();
    }
    
    public void setRecommendID(String rmdID){
    	editor.putString(RCMID, rmdID);
    	editor.commit();
    }
    
    public String getRecommendID(){
    	String rmdID=mPreferences.getString(RCMID, "4");
    	return rmdID;
    }
    
    public void setUserID(int userId){
    	editor.putInt(ID, userId);
    	editor.commit();
    }
    
    public int getUserID(){
    	int userId =mPreferences.getInt(ID, -1);
    	return userId;
    }

   
}
